const SECOND = 1000;
const MINUTE = 60 * SECOND;
const HOUR = 60 * MINUTE;
const DAY = 24 * HOUR;

// 小点整数部分
export const decimalRounding = value => {
  return value.split('.')[0];
};

// 小点整数部分
export const decimal = value => {
  const minVal = String(value).split('.')[1];

  return minVal ? `.${minVal}` : '';
};

// 分——元
export const changeSub = value => {
  return (value / 100).toFixed(2) || '0.00';
};

// 总小时数
export const timeFilter = (time, type) => {
  const days = Math.floor(time / DAY);
  const hours = Math.floor((time % DAY) / HOUR);
  const minutes = Math.floor((time % HOUR) / MINUTE);
  const seconds = Math.floor((time % MINUTE) / SECOND);

  if (type === 'seconds') {
    return (+seconds >= 10 ? seconds : `0${seconds}`) || '00';
  }
  if (type === 'minutes') {
    return (+minutes >= 10 ? minutes : `0${minutes}`) || '00';
  }
  if (type === 'hours') {
    return (+hours >= 10 ? hours : `0${hours}`) || '00';
  }
  if (type === 'days') {
    return (+days >= 10 ? days : `0${days}`) || '00';
  }
  if (type === 'totalHours') {
    return (days * 24 + hours >= 10 ? days * 24 + hours : `0${hours}`) || '00';
  }
  return '00';
};

export default {
  install(Vue) {
    Vue.filter('decimalRounding', decimalRounding);
    Vue.filter('decimal', decimal);
    Vue.filter('timeFilter', timeFilter);
    Vue.filter('changeSub', changeSub);
  },
};
